package com.junzhiit.nengji.shop.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.base.spring.Context;
import com.bainaeco.framework.util.serializer.Format;
import com.junzhiit.nengji.bo.OrdersGoodsBO;
import com.junzhiit.nengji.bo.OrdersGoodsInfoBO;
import com.junzhiit.nengji.dao.GoodsDAO;
import com.junzhiit.nengji.dao.OrdersDAO;
import com.junzhiit.nengji.dao.OrdersGoodsDAO;
import com.junzhiit.nengji.model.Cart;
import com.junzhiit.nengji.model.Orders;
import com.junzhiit.nengji.model.OrdersGoods;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.OrderGoodsStatus;
import com.junzhiit.nengji.shop.service.OrdersGoodsService;
import com.junzhiit.nengji.shop.web.exception.OrdersGoodsNotFoundException;
import com.junzhiit.nengji.shop.web.vo.OrdersGoodsVO;
import com.junzhiit.nengji.shop.web.vo.ResultVO;

@Service
public class OrdersGoodsServiceImpl extends Context implements OrdersGoodsService {

	@Autowired
	private OrdersGoodsDAO ordersGoodsDAO;

	@Autowired
	private GoodsDAO goodsDAO;

	@Autowired
	private OrdersDAO ordersDAO;

	public List<Cart> add(String goods, String orderId, String userId, String shopId) {
		String[] goodsArray = goods.split(";");
		Double total = new Double(0);
		for (int i = 0; i < goodsArray.length; i++) {
			String[] goodsInfo = goodsArray[i].split("_");

			Double number = Double.parseDouble(goodsInfo[0]);
			Double price = Double.parseDouble(goodsInfo[1]);
			total += (number * price);

		}
		try {
			List<Cart> cartList = new ArrayList<Cart>();
			for (int i = 0; i < goodsArray.length; i++) {

				String[] goodsInfo = goodsArray[i].split("_");// number_price_goodsId_skuContentId
				OrdersGoodsInfoBO ordersGoodsInfoBO;
				if (goodsInfo.length == 4) {
					ordersGoodsInfoBO = goodsDAO.findGoodsIdIsSku(userId, shopId, goodsInfo[3]);
				} else {
					ordersGoodsInfoBO = goodsDAO.findGoodsId(userId, shopId, goodsInfo[2]);
				}
				OrdersGoods orderGoods = new OrdersGoods();
				BeanUtils.copyProperties(ordersGoodsInfoBO, orderGoods);
				orderGoods.setOrderId(orderId);
				orderGoods.setStatus(OrderGoodsStatus.BUY.toIntValue());
				if (ordersGoodsInfoBO.getIsOpenSku() == Is.NO.toIntValue()) {
					orderGoods.setPrice(Double.parseDouble(goodsInfo[1]));

				}

				Orders params = ordersDAO.query(orderId);

				if (StringUtils.isNoneBlank(params.getUserCouponsId())) {
					if (ordersGoodsInfoBO.getIsOpenSku() == Is.NO.toIntValue()) {
						Double money = Format.decimal((Double.parseDouble(goodsInfo[1]) * Integer.valueOf(goodsInfo[0]))
								/ total * params.getCouponsMoney());
						orderGoods.setCouponsMoney(money);
					} else {
						Double money = Format.decimal((ordersGoodsInfoBO.getPrice() * Integer.valueOf(goodsInfo[0])) / total * params.getCouponsMoney());
						orderGoods.setCouponsMoney(money);
					}

				}

				orderGoods.setNumber(Integer.parseInt(goodsInfo[0]));
				orderGoods.setProfit(ordersGoodsInfoBO.getProfit());
				orderGoods = ordersGoodsDAO.insert(orderGoods);

				// 返回信息用于删除购物车的商品
				Cart cart = new Cart();
				cart.setGoodsId(orderGoods.getGoodsId());
				cart.setSkuContentId(orderGoods.getSkuContentId());
				cartList.add(cart);
			}
			return cartList;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}

	@Override
	public ResultVO getNotEvaluateOrderGoods(String orderId) {
		ResultVO result = new ResultVO();
		List<OrdersGoodsBO> list = ordersGoodsDAO.getNotEvaluateOrderGoods(orderId);
		if (list.size() <= 0) {
			throw new OrdersGoodsNotFoundException("订单商品没找到");
		}
		List<OrdersGoodsVO> listVO = new ArrayList<OrdersGoodsVO>();
		for (OrdersGoodsBO ordersGoodsBO : list) {
			OrdersGoodsVO ordersGoodsVO = new OrdersGoodsVO();
			BeanUtils.copyProperties(ordersGoodsBO, ordersGoodsVO);
			listVO.add(ordersGoodsVO);
		}
		result.setData(listVO);
		return result;
	}

}
